// pages/app/certification/review/review.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    themeColor2: '',
    defaultColor: '',
    borderColor: '',
    list: [],
    currNo: 0,
    answers: [],
    wrongList: [],

    // 底部提示
    answerList: [],
    correctList: []
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      wrongList: app.globalData.lastWrongList
    })

    // 生成错题列表
    var list1 = new Array;
    var list2 = new Array;
    for (var i = 0; i < self.data.wrongList.length; i ++) {
      var index = self.data.wrongList[i];
      list1.push(app.globalData.lastQuestions[index]);
      list2.push(app.globalData.lastAnswers[index]);
    }

    self.setData({
      list: list1,
      answers: list2
    });

    // 处理底部提示
    var answers = self.data.answers;
    var questions = self.data.list;
    var answerList = new Array;
    var correctList = new Array;
    for (var i = 0; i < questions.length; i ++) {
      var question = questions[i];
      var answer = answers[i];
      
      if (question.type == 1) {
        for (var j = 0; j < question.answers.length; j ++) {
          if (question.answers[j].isAnswer == 1) {
            if (j == 0) {
              correctList.push("A");
            } else if (j == 1) {
              correctList.push("B");
            } else if (j == 2) {
              correctList.push("C");
            } else if (j == 3) {
              correctList.push("D");
            }
          }
        }

        if (answer === 0) {
          answerList.push("A");
        } else if (answer == 1) {
          answerList.push("B");
        } else if (answer == 2) {
          answerList.push("C");
        } else if (answer == 3) {
          answerList.push("D");
        }
        
      } else if (question.type == 2) {
        var correctAnswer = '';
        for (var j = 0; j < question.answers.length; j++) {
          if (question.answers[j].isAnswer == 1) {
            if (j == 0) {
              correctAnswer += 'A';
            } else if (j == 1) {
              correctAnswer += 'B';
            } else if (j == 2) {
              correctAnswer += 'C';
            } else if (j == 3) {
              correctAnswer += 'D';
            }
          }
        }
        correctList.push(correctAnswer);

        var answerStr = '';
        for (var j = 0; j < answer.length; j ++) {
          if (answer[j] == 1 && j == 0) {
            answerStr += 'A';
          } else if (answer[j] == 1 && j == 1) {
            answerStr += 'B';
          } else if (answer[j] == 1 && j == 2) {
            answerStr += 'C';
          } else if (answer[j] == 1 && j == 3) {
            answerStr += 'D';
          }
        }
        answerList.push(answerStr);
      }
    }
    self.setData({
      correctList: correctList,
      answerList: answerList
    })
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      themeColor2: app.globalData.themeColor2,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  prevQuest(event) {
    if (self.data.currNo > 0) {
      self.setData({
        currNo: self.data.currNo - 1
      })
    }
  },
  nextQuest(event) {
    if (self.data.currNo < self.data.list.length) {
      self.setData({
        currNo: self.data.currNo + 1
      })
    }
  }
})